package com.gosophia.reportData.action;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.flowForm.entity.FlowForm;
import com.gosophia.flowForm.service.FlowFormManageService;
import com.gosophia.flowFormManagement.dto.FlowFormDTO;
import com.gosophia.flowFormManagement.dto.FormInstanceDTO;
import com.gosophia.flowFormManagement.ws.FormInstanceWebService;
import com.gosophia.reportData.dto.ApprovalReportJobDTO;
import com.gosophia.reportData.dto.LaunchReportJobDTO;
import com.gosophia.reportData.dto.ReportJobDTO;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.reportData.ws.ReportJobWebService;
import com.gosophia.reportData.ws.impl.ReportJobWebServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 上报操作
 * 
 * @author tianchunyu
 * 
 */
@Controller
@Scope("prototype")
public class ReportAction extends BaseSupportAction {

	private static final Log logger = LogFactory
			.getLog(ReportJobWebServiceImpl.class);
	private Long jobId;
	private static final long serialVersionUID = 1L;
	private LaunchReportJobDTO launchReportDTO;
	@Autowired
	private FlowFormManageService flowFormManageService;
	@Autowired
	private LaunchReportWebService launchReportWebService;
	@Autowired
	private ReportJobWebService reportJobWebService;

	@Autowired
	private FormInstanceWebService formInstanceWebService;
	private FlowForm flowForm;
	private static final String F = "F";// 附件上报
	private static final String B = "B";// 表单上报
	private static final Character EXCEL = 'E';// excel上报
	private static final Character FLOW = 'F';// 电子表单上报上报
	private ApprovalReportJobDTO approvalReportJobDTO;
	private Map<String, Object> newDataInstance;

	private String fromFlag;

	public String _execute() throws Exception {
		/**
		 * 附件上报页面
		 */
		boolean isFirst = reportJobWebService.isFirstReport(jobId);

		/**
		 * 第一次上报
		 */
		launchReportDTO = launchReportWebService.findFirstJobByJobId(jobId);
		if (isFirst) {
			// logger.info("附件:"+launchReportDTO.getAttachmentDtos().size());
			logger.info("任务名称:"
					+ launchReportDTO.getMissionDTO().getMissionName()
					+ ";任务编号:"
					+ launchReportDTO.getMissionDTO().getMissionCode());
			if (launchReportDTO.getReportMethod().equals(F)) {
				return "annex";
			}

			/**
			 * 报表上报
			 */
			else if (launchReportDTO.getReportMethod().equals(B)) {
				flowForm = flowFormManageService
						.findFlowFormById(launchReportDTO.getFlowFormDto()
								.getFormId());
				if (flowForm.getModel().equals(EXCEL)) {
					return "formDataReport";
				} else if (flowForm.getModel().equals(FLOW)) {
					return "flowFormReport";
				}
			}
		} else {
			approvalReportJobDTO = launchReportWebService
					.findPreviousReportByJobId(jobId);
			if (launchReportDTO.getReportMethod().equals(F)) {
				return "annex_";
			}
			/**
			 * 报表上报
			 */
			else if (launchReportDTO.getReportMethod().equals(B)) {
				flowForm = flowFormManageService
						.findFlowFormById(launchReportDTO.getFlowFormDto()
								.getFormId());
				if (flowForm.getModel().equals(EXCEL)) {
					return "formDataReport_";
				} else if (flowForm.getModel().equals(FLOW)) {
					// FormInstanceDTO formInstanceDTO = formInstanceWebService
					// .findFormInstanceById(approvalReportJobDTO
					// .getReportJobDto().getFormInstanceId());
					// Map<Long, Object> dataInstance = formInstanceDTO
					// .getInstanceData();
					// newDataInstance = reportJobWebService
					// .findFlowFormData(dataInstance);
					return "flowFormReport_";
				}
			}
		}
		logger.info("附件:" + launchReportDTO.getAttachmentDtos().size());
		logger.info("任务名称:" + launchReportDTO.getMissionDTO().getMissionName()
				+ ";任务编号:" + launchReportDTO.getMissionDTO().getMissionCode());

		return ERROR;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public FlowForm getFlowForm() {
		return flowForm;
	}

	public void setFlowForm(FlowForm flowForm) {
		this.flowForm = flowForm;
	}

	public ApprovalReportJobDTO getApprovalReportJobDTO() {
		return approvalReportJobDTO;
	}

	public void setApprovalReportJobDTO(
			ApprovalReportJobDTO approvalReportJobDTO) {
		this.approvalReportJobDTO = approvalReportJobDTO;
	}

	public LaunchReportJobDTO getLaunchReportDTO() {
		return launchReportDTO;
	}

	public void setLaunchReportDTO(LaunchReportJobDTO launchReportDTO) {
		this.launchReportDTO = launchReportDTO;
	}

	public Map<String, Object> getNewDataInstance() {
		return newDataInstance;
	}

	public void setNewDataInstance(Map<String, Object> newDataInstance) {
		this.newDataInstance = newDataInstance;
	}

	public String getFromFlag() {
		return fromFlag;
	}

	public void setFromFlag(String fromFlag) {
		this.fromFlag = fromFlag;
	}

}
